package com.qcby.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qcby.dao.StudentDao;
import com.qcby.entity.*;
import com.qcby.service.ClassNoteService;
import com.qcby.service.DeepSeekService;
import com.qcby.service.MessageService;
import com.qcby.service.StudentService;
import com.qcby.util.WordWriter;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private DeepSeekService deepSeekService;
    @Autowired
    private ClassNoteService classNoteService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private WordWriter wordWriter;

    @Override
    public Result login(Student student, HttpServletRequest request) {
        Student student1 = studentDao.login(student);
        if (student1 != null) {
            HttpSession session = request.getSession();
            session.setAttribute("student", student1);
            return Result.success(student1);
        } else {
            return Result.error("用户名或密码错误");
        }
    }

    @Override
    public PageResult<Student> list(Student student) {
        PageHelper.startPage(student.getPage(), student.getPageSize());
        List<Student> page1 =  studentDao.list(student);
        System.out.println(page1);
        Page<Student> page2 = (Page<Student>) page1;
        PageResult<Student> pageResult = new PageResult<>(page2.getTotal(),page2.getResult());
        return pageResult;
    }

    @Override
    public void addstudent(Student student) {
        student.setPassword("123456");
        studentDao.addstudent(student);
    }

    @Override
    public void deletestudent(Integer id) {
        studentDao.deletestudent(id);
    }

    @Override
    public void updatestudent(Student student) {
        studentDao.updatestudent(student);
    }

    @Override
    public List<Student> findMessage(Student student) {
        return studentDao.findMessage(student);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Map<String, String> AnalyzeAllStudentInfo(Student student) {
//        定义一个Map集合，存放返回结果
        Map<String,String> result = new HashMap<>();
//        查到所有关于该学生的信息
//        1、个人信息
        List<Student> list = studentDao.findAllMessage(student);
//        2、笔记信息
        List<ClassNote> notelist = classNoteService.getNoteByStudentName(student);
//        3、聊天信息
        List<Message> messagelist = messageService.findMessageByStudentName(student);
        String str = "这是一个关于学生所有的信息，包括姓名、密码、年龄、性别、学号、手机号、班级名、头像、聊天记录、笔记名称，以及笔记名称对应里面的内容";
        str+="姓名："+list.get(0).getName()+"，密码："+list.get(0).getPassword()+"，年龄："+list.get(0).getAge()+"，性别："+list.get(0).getSex()+"，学号："+list.get(0).getSno()+"，手机号："+list.get(0).getPhone()+"，班级名："+list.get(0).getClassName()+"，头像："+list.get(0).getIcon();
        str+="笔记：";
        for (ClassNote note : notelist) {
            str+="笔记名称："+note.getName()+"，笔记内容："+note.getContent();
        }
        str+="聊天记录：";
        for (Message message : messagelist) {
            str+="，发送内容："+message.getText();
        }
        str+="对该学生进行分析，并且做出一些学习上的建议，大概200~300字";
        System.out.println(str);
        String res = "";
        try {
            String AIresult = deepSeekService.getJson(str);
            JSONObject jsonObject = new JSONObject(AIresult);
//            AI分析的结果
            res = jsonObject.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
//            AI结果返回
            String url = wordWriter.uploadDoc(res, student.getName() + "的分析结果.docx");
            result.put("url", url);
            result.put("res", res);
            student.setPdf(url);
            student.setPaper(res);
//            保存到数据库
            studentDao.updatestudentAI(student);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Integer findRepeatMessage(Student student) {
        return studentDao.findRepeatMessage(student);
    }

    @Override
    public List<Integer> findAllStudentIds() {
        return studentDao.findAllStudentIds();
    }

    @Override
    public PageResult<Student> list1(Student student) {
        PageHelper.startPage(student.getPage(), student.getPageSize());
        List<Student> page1 =  studentDao.list1(student);
        System.out.println(page1);
        Page<Student> page2 = (Page<Student>) page1;
        PageResult<Student> pageResult = new PageResult<>(page2.getTotal(),page2.getResult());
        return pageResult;
    }
}
